{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from skimage import io\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import math\n",
    "\n",
    "def get_center_point_img(img):\n",
    "    h, w = img.shape[0], img.shape[1]\n",
    "    x1 = round(w/2)\n",
    "    y1 = round(h/2)\n",
    "    return x1, y1\n",
    "\n",
    "def crop_image(img, destination, file_name, size = 64):\n",
    "    h, w = img.shape[0], img.shape[1]\n",
    "    x, y = get_center_point_img(img)\n",
    "    f_n = os.path.splitext(file_name)\n",
    "    \n",
    "    w_f = math.floor(w / size)\n",
    "    h_f = math.floor(h / size)\n",
    "    x_center, y_center = get_center_point_img(img)\n",
    "    for i in range(h_f):\n",
    "        for j in range(w_f):\n",
    "            tmp = img[(i*size):(i*size+size), (j*size):(j*size+size), :]\n",
    "            cv2.imwrite(os.path.join(destination, f_n[0]+\"_patch_128x128_\" + str(i) + \"_\" + str(j) + f_n[1]), tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of images: 100\n",
      "number of images: 200\n",
      "number of images: 300\n",
      "357\n"
     ]
    }
   ],
   "source": [
    "DES = \"/home/liem/hai/v3_train_id/64x64/training/natural/\"\n",
    "BASE = \"/home/liem/hai/recapture_classification/v3_train_id/data/training/natural/\"\n",
    "i = 0\n",
    "for item in os.listdir(BASE):\n",
    "    img = cv2.imread(os.path.join(BASE, item))\n",
    "    crop_image(img, DES, item, 128)\n",
    "    i+=1\n",
    "    if i % 100 == 0:\n",
    "        print(\"number of images: \" + str(i))\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
